package com.yulebaby.callsenter.utils;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;

import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.global.App;

import java.io.IOException;

/**
 * Author: Zhangbo
 * Time: 2017/1/5
 * 播放文件的工具类,调用时需要保持生命周期与调用周期的一致.
 */
public class MediaPlayerUtils {

    private static MediaPlayer mPlayer = new MediaPlayer();
    private static MediaPlayerUtils mediaPlayerUtils = new MediaPlayerUtils();
    private CompletionListener listener;

    public static MediaPlayerUtils getInstances() {
        return mediaPlayerUtils;
    }

    /**
     * 打开一个音频
     *
     * @param recordPath
     */
    public void openAudio(String recordPath) {
        Intent i = new Intent("com.android.music.musicservicecommand");
        i.putExtra("command", "pause");
        App.mContext.sendBroadcast(i);
        release();
        try {
            mPlayer = new android.media.MediaPlayer();
            mPlayer.setDataSource(recordPath);
            mPlayer.setOnPreparedListener(mOnPreparedListener);
            mPlayer.setOnCompletionListener(mOnCompletionListener);
            mPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打开一个网络音频
     *
     * @param uri
     */
    public void openAudio(Uri uri) {
        Intent i = new Intent("com.android.music.musicservicecommand");
        i.putExtra("command", "pause");
        App.mContext.sendBroadcast(i);
        release();
        try {
            mPlayer = new android.media.MediaPlayer();
            mPlayer.setDataSource(App.mContext, uri);
            mPlayer.setOnPreparedListener(mOnPreparedListener);
            mPlayer.setOnCompletionListener(mOnCompletionListener);
            mPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断音频是否正在播放
     */
    public boolean isPlaying() {
        if (mPlayer != null) {
            return mPlayer.isPlaying();
        }
        return false;
    }

    /**
     * 开始播放
     */
    public void start() {
        if (mPlayer != null) {
            mPlayer.start();   // 播放音频
        }
    }

    /**
     * 暂停播放
     */
    public void pause() {
        if (mPlayer != null) {
            mPlayer.pause();
        }
    }

    public void release() {
        if (mPlayer != null) {
            mPlayer.reset();
            mPlayer.release();
            mPlayer = null;
        }
    }


    MediaPlayer.OnPreparedListener mOnPreparedListener = mp -> start();

    MediaPlayer.OnCompletionListener mOnCompletionListener = mp -> {
        Logger.d("onCompletion" + mp);
        start();
        if (listener != null) {
            listener.Completion();
        }
        release();
    };

    public void setOnCompletionListener(CompletionListener listener) {
        this.listener = listener;
    }

    public interface CompletionListener {
        void Completion();
    }
}
